<?
require_once("include.php");

$me=false;
require_once("header.session.php");
require_once("header.gui.php");
require_once("header.misc.php");
require_once("header.admin.php");


//globals
$MenuObjects= $me->getMyMenu();

global $me;
if(!$me) {
$me=&new Person($_SESSION['id']);
}
if(isset($_POST['action'])) {
	switch($_POST['action']) {
		case "addtogroup":
			$user = new Person($_POST['addtogroup']);
			$user->addToGroup($_POST['gid']);
			header("location: " . $_SERVER['PHP_SELF'] . "?action=edit&id=" . $_POST['gid']);
			die;
			break;
		case "removefromgroup":
			$user = new Person($_POST['removefromgroup']);
			$user->removeFromGroup($_POST['gid']);
			header("location: " . $_SERVER['PHP_SELF'] . "?action=edit&id=" . $_POST['gid']);
			die;
			break;
		case "removenav":
			$group=new PeopleGroup($_POST['gid']);
			$group->removeNav_id($_POST['page']);
			header("location: " . $_SERVER['PHP_SELF'] . "?action=edit&id=" . $_POST['gid']);
			die;
			break;
		case "addtonav":
			$group=new PeopleGroup($_POST['gid']);
			$group->addNav_id($_POST['page']);
			header("location: " . $_SERVER['PHP_SELF'] . "?action=edit&id=" . $_POST['gid']);
			die;
			break;
		case "changeaccess":
			$group=new PeopleGroup($_POST['gid']);
			$group->changeAccess($_POST['page'],$_POST['access']);
			header("location: " . $_SERVER['PHP_SELF'] . "?action=edit&id=" . $_POST['gid']);
			die;
			break;
		case "changename":
			$group=new PeopleGroup($_POST['gid']);
			$group->changeName($_POST['name']);
			header("location: " . $_SERVER['PHP_SELF'] . "?action=edit&id=" . $_POST['gid']);
			die;
			break;
		default:
			customError("Invalid action post data (" . $_POST['action'] . ") in group management...");
			break;
	}
}

if(!isset($_GET['action'])) {
	$_GET['action']="defaultmenu";
}

switch($_GET['action']) {
	case "newgroup":
		header("Location: " . $_SERVER['PHP_SELF'] . "?action=edit&id=" . newGroup());
		die;
		break;
}
	
function Headers() {
	
}
function Body() {

	switch($_GET['action']) {
		case "edit":
			require_once("editGroup.php");
			echo "<br>
<a href=\"" . $_SERVER['SCRIPT_NAME'] . "\"><- Back</a>
";
			break;
		case "delete":
			$group=groupInfo($_GET['id']);
			if(!$group) {
				echo "That doesn't exist!<br>
<a href=\"" . $_SERVER['SCRIPT_NAME'] . "\"><- Back</a>
";
			} else {
				echo "Are you sure you want to delete the group: " . stripslashes($group['people_group_name']) . "\n" . 
				"<br><a href=\"" . $_SERVER['SCRIPT_NAME'] . "\">NO</a> | <a href=\"" . $_SERVER['SCRIPT_NAME'] . "?action=sdelete&id=" . $group['people_group_id'] . "\">YES</a>";
			}
			break;
		case "sdelete":
			$group=groupInfo($_GET['id']);
			if(!$group) {
				echo "That doesn't exist!<br>";
			} else {
				deleteGroup($_GET['id']);
				echo "The group has been deleted!";
			}
			echo "<br>
<a href=\"" . $_SERVER['SCRIPT_NAME'] . "\"><- Back</a>
";
			break;
		default:
	
	
?>
Welcome to group administration. Here you can find all of the administrative tools for managing 
user groups. 
<br><br>
<form action="group_management.php" method="post">
<table border="0">
<tr><th>
Group Name
</th><th>
Actions
</th></tr>
<?
connectToDB();
$group_list=getGroupList(true);
foreach($group_list as $people_group) {
	echo "<tr><td>" . stripslashes($people_group['people_group_name']) . "\n</td><td>\n" . 
	"<a href=\"group_management.php?action=delete&id=" . $people_group['people_group_id'] . 
	"\">Delete</a> | " . 
	"<a href=\"group_management.php?action=edit&id=" . $people_group['people_group_id'] . 
	"\">Edit</a>\n</td></tr>";
}
echo "</table>
<a href=\"" . $_SERVER['PHP_SELF'] . "?action=newgroup\">Create New Group</a>";
		break;
	}
}


include("template.php");